<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>库存管理</cite></a>
        <a><cite>库存预警</cite></a>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">
            库存预警
        </div>
        <div class="layui-card-body">
            <div class="layui-row" style="margin-bottom: 10px;">
                <form class="layui-form" lay-filter="formSearch">
                    <button type="button" class="layui-btn" id="alert">发送预警短信</button>
                </form>
            </div>
            <!-- 表格 -->
            <table id="alert-table-parseData" lay-filter="alert-table-parseData"></table>
        </div>
    </div>
</div>


<!-- 新增按钮弹出框 -->
<script type="text/html" id="storeManager">
    <form class="layui-form" lay-filter="formAdd">
        <div class="layui-row">
            <div class="layui-col-md8 layui-col-md-offset1">
                <div class="layui-form-item" style="margin-top: 10px;">
                    <label class="layui-form-label">库管员:</label>
                    <div class="layui-input-block">
                        <select name="goodsuuid" id="storeManagers" lay-verify="">
                            <option value="">---请选择库管员---</option>
                        </select>
                    </div>
                </div>
            </div>

        </div>
        <div class="layui-row">
            <div class="layui-form-item">
                <div class="layui-col-md8 layui-col-md-offset4">
                    <div class="layui-form-item">
                        <button type="button" class="layui-btn" onclick="sendMessage()">发送短信</button>
                        <button type="button" class="layui-btn layui-btn-primary closeLayer" onclick="closeLayer()">取消
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </form>
</script>

<script>
    var arr = [];
    layui.use(['admin', 'table', 'form', 'laydate'], function () {
        var admin = layui.admin
            , table = layui.table
            , form = layui.form
            , baseUrl = layui.setter.baseUrl
            , $ = layui.$

        // 表格渲染        
        form.render(null, 'formSearch')
        var tableIns = table.render({
            elem: '#alert-table-parseData'
            // 请求地址
            , url: baseUrl + 'storealert'
            , toolbar: true
            , title: '库存预警'
            , cellMinWidth: 120
            // 是否开启加载loding
            , loading: true
            // 数据渲染规定
            , cols: [[
                {field: 'uuid', title: '仓库编号', width: 110}
                , {field: 'name', title: '商品名称'}
                , {field: 'storenum', title: '库存数量'}
                , {field: 'outnum', title: '待发货数量'}

            ]]
            // 开启分页
            , page: true
            , request: {
                pageName: 'page',
                limitName: 'rows'
            }
            , response: {
                // statusName: 'code',
                statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
            }
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.rows //解析数据列表
                };
            }
        });


        // 发送预警短信
        // $('#sendMessage').click(function () {
        //     var tel = document.getElementById("storeManagers")
        //     alert(tel.value)
        //     admin.req({
        //         url: baseUrl + 'sendMsg',
        //         type: 'get',
        //         data: 'tel.value',
        //         success: function (res) {
        //             console.log(res)
        //             //请求成功回调
        //         }
        //     })
        // })


        // 新增按钮弹出框
        $('#alert').click(function () {
            layer.closeAll();
            layer.open({
                title: "预警",
                type: 1,
                move: false,
                area: ['500px', '285px'],
                content: $('#storeManager').html()
            });

            admin.req({
                url: baseUrl + 'getStoreManager',
                success: function (res) {
                    $.each(res.data, function (i, item) {
                        $('#storeManagers').append(`<option value="${item.tel}">${item.username}</option>`)
                    })
                    form.render(null, 'formAdd');
                }
            })
        })





    });
    // 新增功能提交
    function sendMessage() {

        var i = layer.confirm('是否发送？', {
            btn: ['确认', '取消'] //可以无限个按钮
        }, function (index, layero) {
            var data = document.getElementById("storeManagers")
            layui.admin.req({
                url: layui.setter.baseUrl + 'sendMsg',
                type: 'get',
                data: data.value,
                done: function (res) {
                    layer.closeAll();
                    layer.open({
                        title: '发送短信'
                        , content: res.msg
                    });
                    // layui.table.reload('inventory-table-parseData');
                }
            })
        }, function (index) {
            layer.close(i);
        });

    }
    // 关闭弹框
    function closeLayer() {
        layer.closeAll();
    }

</script>

<style>
    .layui-col-md3,
    .layui-col-md6,
    #reset-btn,
    #search-btn {
        margin-top: 10px;
    }
</style>